package com.idanchuang.count.controller;

import com.idanchuang.component.base.JsonResult;
import com.idanchuang.count.core.annotation.UserCountUpdate;
import com.idanchuang.count.core.constant.UserCountEventTypeEnum;
import com.idanchuang.count.service.UserCountService;
import com.idanchuang.user.count.service.api.client.UserCountServiceClient;
import com.idanchuang.user.count.service.api.request.command.count.UserMultiScenesCountResetCommand;
import com.idanchuang.user.count.service.api.request.command.count.UserMultiScenesCountUpdateCommand;
import com.idanchuang.user.count.service.api.request.command.count.UserSceneCountUpdateCommand;
import com.idanchuang.user.count.service.api.request.query.count.UserCountBatchQuery;
import com.idanchuang.user.count.service.api.request.query.count.UserCountQuery;
import com.idanchuang.user.count.service.api.response.UserMultiScenesCountDTO;
import com.idanchuang.user.count.service.api.response.UserSceneCountDTO;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * controller 层
 * @author Richard_yyf
 * @version 1.0 2021/5/21
 */
@RestController
public class UserCountServiceController implements UserCountServiceClient {

    @Resource
    private UserCountService userCountService;

    @Resource
    private ParamsChecker paramsChecker;

    @Override
    @UserCountUpdate(type = UserCountEventTypeEnum.UserSceneCountUpdate)
    public JsonResult<Void> count(@Valid UserSceneCountUpdateCommand command) {
        userCountService.count(Collections.singletonList(command));
        return JsonResult.OK;
    }

    @Override
    @UserCountUpdate(type = UserCountEventTypeEnum.UserMultiScenesCountUpdate)
    public JsonResult<Void> multiCount(@Valid UserMultiScenesCountUpdateCommand command) {

        paramsChecker.checkUpdateBatchSize(command.getSceneCountIncrements().size());

        userCountService.multiCount(Collections.singletonList(command));

        return JsonResult.OK;
    }

    @Override
    @UserCountUpdate(type = UserCountEventTypeEnum.UserMultiScenesCountReset)
    public JsonResult<Void> batchResetCount(List<UserMultiScenesCountResetCommand> resetCommands) {

        if (CollectionUtils.isEmpty(resetCommands)) {
            return JsonResult.OK;
        }

        paramsChecker.checkResetBatchSize(resetCommands.size());

        userCountService.batchResetCount(resetCommands);

        return JsonResult.OK;
    }

    @Override
    public JsonResult<UserSceneCountDTO> query(@Valid UserCountQuery query) {

        UserSceneCountDTO userSceneCountDTO = userCountService.query(query);

        return JsonResult.success(userSceneCountDTO);
    }

    @Override
    public JsonResult<UserMultiScenesCountDTO> batchQuery(@Valid UserCountBatchQuery query) {

        paramsChecker.checkQueryBatchSize(query.getSceneList().size());

        UserMultiScenesCountDTO userMultiScenesCountDTO = userCountService.batchQuery(query);

        return JsonResult.success(userMultiScenesCountDTO);
    }
}
